##
#
# Snaarpy: Guitar chord parser and formatter
# Copyright (C) 2009 Alberto Gonzalez
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##

'''
Base library for tab file parsing and processing
'''

import logging
logger = logging.getLogger(__name__)

import os.path  

class SnaarpyError(Exception):
    pass

class SnaarpyPlugin(type):
    def __init__(cls, name, bases, attrs):
        if not hasattr(cls, 'plugins'):
            # This branch only executes when processing the mount point itself.
            # So, since this is a new plugin type, not an implementation, this
            # class shouldn't be registered as a plugin. Instead, it sets up a
            # list where plugins can be registered later.
            cls.plugins = {}
        else:
            # This must be a plugin implementation, which should be registered.
            # Simply appending it to the list is all that's needed to keep
            # track of it later.
            cls.plugins[cls.extension] = cls

import snaarpy.output
import snaarpy.input

def process(files, input_formats, output_file=None, output_format='null', sort_by='author'):
    models = []
    for file, format in zip(files, input_formats):
        try:
            model = snaarpy.input.read_file(file, format)
            models.append(model)
        except Exception, ex:
            raise ex
        #    logger.error("Error parsing %s: %s" % (file, ex))
        #    return
        
    models.sort(key=lambda x: getattr(x, sort_by))
    snaarpy.output.write_file(output_file, output_format, models)
